package com.jh.paymentchannel.helipay.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.util.*;

public class HeliPayBase {

	protected final Log logger = LogFactory.getLog(this.getClass());

	public static final String MERCHANT_NO ="C1800001614";
	
	public static final String REQUEST_URL_SAME_PERSON = "http://pay.trx.helipay.com/trx/quickPayApi/interface.action";


//	public static final String REQUEST_URL_SAME_PERSON = "http://192.168.30.136:18182/trx/quickPayApi/interface.action";
//
//	public static final String SIGNKEY_SAME_PERSON="UPkNxZZl473CcjWQubattfyIJwmSuna4";
	
	public static final String SPLIT = "&";

	public static final String REQUEST_URL_TRANSFER = "http://pay.trx.helipay.com/trx/transfer/interface.action";
	
	public static final String SIGNKEY_TRANSFER_RSA = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMSmWXaEdWY4+TdCbqyatmWwzWmvuFXP/HCFFYUHj0SQ7My+YApa0r3KezJeDOxprn3MIDK4b81K8E3kQ8qV0fsC7IZ0Bo2L9zw/9ECXb3ApQNeVC0B9YtHyBH7gzc/yriPi/T5xuIn0RIw9dtaOL1LLYFO6iWBf/hsp7xwgHZ+/AgMBAAECgYEAu/Q/B988knKcCGwPehv5f6KIAp64MhSztBwPYHHERoW6P0sqYltCl7IjJBXuhjvQUl463B+ahH4+xkQJY2TlJzcRu1dxpSOtigWCvtYx1KhBlEV7L+5NfRHLpVfADxgvHOF+Qw4OVqGRMW0uhcT1d+7Qo+dXL/xIlwm0Y6AwPfECQQDoJNMTyrSxNgLVsLaMKeDH5aZzUv4WyjxfCUxipja++HHaaKM3/PwHAeGWdtlv41kPuxD1MPZSpamueROUHCCZAkEA2NvB79XqymeEj5g5Ak5v/QaTCZndA8fvBvw7DJYByPiW+LsaUafiqDJJStxa7FmEEdYOO9Usjvz02+CyQfGCFwJATj1bBqijDbbubacJX2Qd5kF4QBYQnQ28Gyu/GuB6QxLpDHuHxy70okf4CAY2codlVO/vXzQJUQGUlsEn4Bs6wQJAZXV7qQBGl2gTRzOLCJ82GgWnaYiJNSmnlXcREXRlZWeZCspPKAEKNHNCgznReF7xQX8Krcewzz1eOjlIbT3XBQJBAJotViihYAanJBJ7XWN5bgLIo6Gs3yhoiBppbaAEeziwgtjOY1YFllKwqfyHiF/VXbvzdxPg/nemsm5mfLCxLm8=";
	
	public static final String SIGNKEY_SAME_PERSON = "0GeaCrsd0CSjDguv7SAToqslz9tqWOTb";
	
    public static final String SIGNKEY_TRANSFER_MD5 = "dLgpjqlrY9OZkocEJUBKD8l6An3pens2";
	
	
	public Map<String, Object> getPostJson(HttpServletRequest request) throws IOException {
		java.io.ByteArrayOutputStream inBuffer = new java.io.ByteArrayOutputStream();
		java.io.InputStream input = request.getInputStream();
		byte[] tmp = new byte[1024];
		int len = 0;
		while ((len = input.read(tmp)) > 0) {
			inBuffer.write(tmp, 0, len);
		}
		byte[] requestData = inBuffer.toByteArray();
		String requestJsonStr = new String(requestData, "UTF-8");
		logger.info(requestJsonStr);
		JSONObject requestJson = JSON.parseObject(requestJsonStr);
		logger.info(requestJson);
		return parseJSON2Map(requestJson);
	}
	
	public Map<String, Object> parseJSON2Map(JSONObject json) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (json != null) {
			for (Object k : json.keySet()) {
				Object v = json.get(k);
				// 如果内层还是数组的话，继续解析
				if (v instanceof JSONArray) {
					List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
					Iterator<Object> it = ((JSONArray) v).iterator();
					while (it.hasNext()) {
						JSONObject json2 = (JSONObject) it.next();
						list.add(parseJSON2Map(json2));
					}
					map.put(k.toString(), list);
				} else {
					map.put(k.toString(), v);
				}
			}
		}
		logger.info(map);
		return map;
	}
}
